#include <QtGui>
#include <QtOpenGL>

#include <gl/GLU.h>
#include <math.h>

#include "glwidget.h"

GLWidget::GLWidget(QWidget *parent)
	: QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
{
	xRot = 0;
	yRot = 0;

	zCam = -10;
}

GLWidget::~GLWidget()
{
}

QSize GLWidget::minimumSizeHint() const
{
	return QSize(50, 50);
}

QSize GLWidget::sizeHint() const
{
	return QSize(400, 400);
}

void GLWidget::initializeGL()
{
	qglClearColor( Qt::black );

	glEnable(GL_DEPTH_TEST);
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glEnable(GL_COLOR_MATERIAL);
	glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);

	GLfloat light_ambient[] = { 0.1f, 0.1f, 0.1f, 1.0f };
	GLfloat light_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
	GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
	GLfloat light_position[] = { 1.0f, 1.0f, 0.33f, 0.0f };

	glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
	glLightfv(GL_LIGHT0, GL_POSITION, light_position);

	glMaterialfv(GL_FRONT, GL_DIFFUSE, light_diffuse);
	glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}

void GLWidget::paintGL()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	// model view matrix
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(0,0,zCam,0,0,0,0,1,0);

	glRotatef(xRot,1,0,0);
	glRotatef(yRot,0,1,0);

	// draw the scene:
	glColor3f(1.0,1.0,1.0);
	
	// draw point in middle
	glBegin(GL_TRIANGLES);
		// front-facing triangle
		glNormal3f(0,1,1);
		glVertex3f(0,1,0);
		glVertex3f(-1,-1,-1);
		glVertex3f(1,-1,-1);

		// back-facing triangle
		glNormal3f(0,1,-1);
		glVertex3f(0,1,0);
		glVertex3f(1,-1,1);
		glVertex3f(-1,-1,1);

		// left-facing triangle
		glNormal3f(-1,1,0);
		glVertex3f(0,1,0);
		glVertex3f(-1,-1,-1);
		glVertex3f(-1,-1,1);

		// left-facing triangle
		glNormal3f(1,1,0);
		glVertex3f(0,1,0);
		glVertex3f(1,-1,-1);
		glVertex3f(1,-1,1);

		// bottom quad
		glNormal3f(0,0,-1);
		glVertex3f(-1,-1,1);
		glVertex3f(-1,-1,-1);
		glVertex3f(1,-1,-1);
		glVertex3f(-1,-1,1);
		glVertex3f(1,-1,-1);
		glVertex3f(1,-1,1);

	glEnd();
}

void GLWidget::resizeGL(int width, int height)
{
    glViewport( 0, 0, (GLint)width, (GLint)height );

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
	gluPerspective( 45.0, (float)width/(float)height, 0.1, 100.0 );

    glMatrixMode( GL_MODELVIEW );
}

void GLWidget::mousePressEvent(QMouseEvent *event)
{
	lastPos = event->pos();
}

void GLWidget::mouseMoveEvent(QMouseEvent *event)
{
	int dx = event->x() - lastPos.x();
	int dy = event->y() - lastPos.y();

	if (event->buttons() & Qt::LeftButton)
	{
		yRot += (float)dx;
		xRot -= (float)dy;

		emit rotationSet(xRot,yRot);

		repaint();
	}
	else if (event->buttons() & Qt::RightButton)
	{

	}
	lastPos = event->pos();
}

void GLWidget::wheelEvent(QWheelEvent *event)
{
	int dw = event->delta();

	zCam += (float)dw*0.01f;

	emit zoomSet(zCam);

	repaint();
}